/**
 * Copyright (C) 2012 The SkyTvOS Project
 *
 * Version     Date           Author
 * ─────────────────────────────────────
 *           2015-2-10         yellowlgx
 *
 */

package com.skyworth.ui.customview;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.FrameLayout;
import android.widget.TextView;

/**
 * @ClassName ShaderTextVeiw
 * @Description TODO (write something)
 * @author yellowlgx
 * @date 2015-2-10
 * @version TODO (write something)
 */
public class ShaderTextView extends FrameLayout
{
    private TextView mTextView;
    private Paint mPaint;// 画笔

    public ShaderTextView(Context context)
    {
        super(context);
        initPaint(context);
    }

    public ShaderTextView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        initPaint(context);
    }

    public ShaderTextView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context, attrs, defStyle);
        initPaint(context);
    }

    private void initPaint(Context context)
    {
        mTextView = new TextView(context);
        mPaint = mTextView.getPaint();
        mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
        this.addView(mTextView, new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT, Gravity.CENTER));
    }

    public void setTextColor(int color)
    {
        mTextView.setTextColor(color);
    }

    /**
     * 设置显示文字内容
     * 
     * @param text
     *            文本内容
     * @date 2015-2-11
     */
    public void setText(String text)
    {
        mTextView.setText(text);
    }

    /**
     * 设置文本大小
     * 
     * @param size
     *            float型大小,int可以强转
     * @date 2015-2-11
     */
    public void setTextSize(float size)
    {
        mTextView.setTextSize(size);
    }

    /**
     * 设置该模块是否需要透明度渐变,包括文字和背景图
     * 
     * @param x0
     *            渐变起始X坐标，默认是从0开始
     * @param y0
     *            渐变起始Y坐标，默认是从0开始
     * @param x1
     *            渐变结束X坐标，根据实际情况来定
     * @param y1
     *            渐变结束Y坐标，根据实际情况来定
     * @param color0
     *            渐变起始颜色，int型
     * @param color1
     *            渐变结束颜色，int型
     * @param bg_id
     *            需要渐变的背景图ID
     * @date 2015-2-11
     */
    @SuppressWarnings("deprecation")
    public void setShader(float x0, float y0, float x1, float y1, int color0, int color1, int bg_id)
    {
        Bitmap res = BitmapFactory.decodeResource(getResources(), bg_id);
        Bitmap overlay = Bitmap.createBitmap(res.getWidth(), res.getHeight(),
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(overlay);
        Paint paint = new Paint();
        paint.setFlags(Paint.FILTER_BITMAP_FLAG);
        paint.setShader(new LinearGradient(x0, y0, x1, y1, color0, color1, Shader.TileMode.CLAMP));
        canvas.drawBitmap(res, 0, 0, null);
        PorterDuffXfermode mXfermode = new PorterDuffXfermode(PorterDuff.Mode.DST_IN);
        paint.setXfermode(mXfermode);
        canvas.drawRect(0, 0, res.getWidth(), res.getHeight(), paint);
        this.setBackgroundDrawable(new BitmapDrawable(overlay));

        mPaint.setShader(new LinearGradient(x0, y0, x1, y1, color0, color1, Shader.TileMode.CLAMP));
        postInvalidate();
    }
}
